package com.itheima.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.tanhua.api.UserApi;
import com.itheima.tanhua.domain.User;
import com.itheima.tanhua.dubbo.mappers.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Lff
 * @date 2022/3/22
 */
@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据手机号查询用户
     *
     * @param mobile
     * @return
     */
    @Override
    public User findByMobile(String mobile) {
        LambdaQueryWrapper<User> qw = Wrappers.lambdaQuery(User.class);
        //设置条件
        qw.eq(User::getMobile, mobile);
        //查询
        User user = userMapper.selectOne(qw);
        return user;
    }

    /**
     * 保存用户信息
     *
     * @param user
     * @return
     */
    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public void updateById(User user) {
        userMapper.updateById(user);
    }

    /**
     * 根据Id查询环信信息
     *
     * @param id
     * @return
     */
    @Override
    public User findHxInfoById(Long id) {
        LambdaQueryWrapper<User> qw = Wrappers.lambdaQuery(User.class);
        //设置条件
        qw.eq(User::getId, id)
                .select(User::getHxUser, User::getHxPassword);
        User user = userMapper.selectOne(qw);
        return user;
    }

    @Override
    public User findById(Long id) {
        return userMapper.selectById(id);
    }
}
